﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Fireball.OpenXML.Word.Readers
{
    public class StyleSectionReader
    {
        private XElement mainElement = null;

        public StyleSectionReader(XElement mainElement)
        {
            this.mainElement = mainElement;
        }

        public StyleSection Parse()
        {
            StyleSection section = new StyleSection();

            if (mainElement.Name.LocalName != "styles")
                throw new Exception("Invalid Root Element");

            XElement defaults = mainElement.Element(mainElement.Name.Namespace + "docDefaults");
            if (defaults != null)
            {
                var docDefaults = ParseDocDefaults(defaults);

                section.Defaults = docDefaults;
            }

            XElement latentStyles = mainElement.Element(NS.WM + "latentStyles");


            if (latentStyles != null)
            {
                section.LatentStyles = LatentStyles.Parse(latentStyles);
            }


            IEnumerable<XElement> styles = mainElement.Elements(NS.WM + "style");

            if (styles != null && styles.Count() > 0)
            {
                var _styles = ParseStyles(styles);

                section.Styles.AddRange(_styles);
            }

            return section;
        }

        private Style[] ParseStyles(IEnumerable<XElement> styles)
        {
            List<Style> stylesList = new List<Style>();

            foreach (XElement xStyle in styles)
            {
                var st = Style.Parse(xStyle);

                if (st != null)
                    stylesList.Add(st);
            }

            return stylesList.ToArray();
        }

        private StyleDocDefaults ParseDocDefaults(XElement defaults)
        {
            StyleDocDefaults docDefaults = new StyleDocDefaults();

            var rPrDefault = defaults.Element(defaults.Name.Namespace + "rPrDefault");

            if(rPrDefault != null)
            {
                var rPr = rPrDefault.Element(NS.WM + "rPr");
                if (rPr != null)
                {
                    docDefaults.RunProperties = StyleRunProperties.Parse(rPr);
                }

                var pPr = rPrDefault.Element(NS.WM + "pPr");

                if (pPr != null)
                {
                    docDefaults.ParagraphProperties = ParagraphProperties.Parse(pPr);
                }
            }

            return docDefaults;
        }

       
    }
}
